package com.mask.roll.edu.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mask.roll.common.code.ServiceEnums;
import com.mask.roll.common.constant.RollConstants;
import com.mask.roll.common.exception.RollException;
import com.mask.roll.edu.entity.Subject;
import com.mask.roll.edu.entity.VO.subject.SubjectExcelVO;
import com.mask.roll.edu.service.SubjectService;
import lombok.extern.slf4j.Slf4j;

/**
 * 课程分类excel监听器
 * @author hx
 * @since 2021/10/23 1:36 下午
 */
@Slf4j
public class SubjectExcelListener extends AnalysisEventListener<SubjectExcelVO> {

    /**
     * 因为SubjectExcelListener不能交给spring进行管理，需要自己new，不能注入其他对象
     * 不能实现数据库操作
     */

    public SubjectService subjectService;
    public SubjectExcelListener() {}
    public SubjectExcelListener(SubjectService subjectService) {
        this.subjectService = subjectService;
    }

    /**
     * 读取excel内容，一行一行进行读取
     * @param subjectExcelVO
     * @param analysisContext
     */
    @Override
    public void invoke(SubjectExcelVO subjectExcelVO, AnalysisContext analysisContext) {
        //文件数据为空判断
        if(subjectExcelVO == null) {
            throw new RollException(ServiceEnums.EXCEL_FILE_NOT_DATA.getCode(),
                    RollConstants.ERROR,ServiceEnums.EXCEL_FILE_NOT_DATA.getMessage());
        }

        //一行一行读取，每次读取有两个值，第一个值一级分类，第二个值二级分类
        //判断一级分类是否重复
        Subject existOneSubject = this.existOneSubject(this.subjectService, subjectExcelVO.getOneSubjectName());
        //没有相同一级分类，进行添加
        if(existOneSubject == null) {
            existOneSubject = new Subject();
            existOneSubject.setParentId("0");

            //一级分类名称
            existOneSubject.setTitle(subjectExcelVO.getOneSubjectName());
            subjectService.save(existOneSubject);
        }

        //获取一级分类id值
        String pid = existOneSubject.getId();

        //添加二级分类
        //判断二级分类是否重复
        Subject existTwoSubject = this.existTwoSubject(this.subjectService, subjectExcelVO.getTwoSubjectName(), pid);
        if(existTwoSubject == null) {
            existTwoSubject = new Subject();
            existTwoSubject.setParentId(pid);

            //二级分类名称
            existTwoSubject.setTitle(subjectExcelVO.getTwoSubjectName());
            subjectService.save(existTwoSubject);
        }
    }

    /**
     * 判断一级分类不能重复添加
     * @param subjectService
     * @param name
     * @return
     */
    private Subject existOneSubject(SubjectService subjectService,String name) {
        QueryWrapper<Subject> wrapper = new QueryWrapper<>();
        wrapper.eq("title",name);
        wrapper.eq("parent_id","0");
        Subject oneSubject = subjectService.getOne(wrapper);
        return oneSubject;
    }

    /**
     * 判断二级分类不能重复添加
     * @param subjectService
     * @param name
     * @param pid
     * @return
     */
    private Subject existTwoSubject(SubjectService subjectService,String name,String pid) {
        QueryWrapper<Subject> wrapper = new QueryWrapper<>();
        wrapper.eq("title",name);
        wrapper.eq("parent_id",pid);
        Subject twoSubject = subjectService.getOne(wrapper);
        return twoSubject;
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        log.info("读取文件结束。。。");
    }
}
